package project.web;

import Utils.data.DataConverters;
import Utils.io.json.JacksonUtils;
import Utils.web.servlet.BaseServlet;
import project.pojo.Book;
import project.pojo.Cart;
import project.pojo.CartItem;
import project.service.BookService;
import project.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "CartServlet", value = "/CartServlet")
public class CartServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     */
    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        int id = DataConverters.parseInt(request.getParameter("id"), 0);
        Book book = bookService.queryBookById( id);
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            request.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);
        request.getSession().setAttribute("latestName", book.getName());
        response.sendRedirect(request.getHeader("Referer"));
    }

    /**
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     */
    protected void addItemAjax(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        int id = DataConverters.parseInt(request.getParameter("id"), 0);
        Book book = bookService.queryBookById( id);
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            request.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);

        Map<String, Object> resultMap=new HashMap<>();
        resultMap.put("cartCount", cart.getCount());
        resultMap.put("latestName", book.getName());
        response.getWriter().write(JacksonUtils.getInstance().serialize(resultMap));
    }

    protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = DataConverters.parseInt(request.getParameter("id"), 0);
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            cart.deleteItem(id);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }
    
    protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            Integer id = DataConverters.parseInt(request.getParameter("id"));
            Integer count = DataConverters.parseInt(request.getParameter("count"));
            cart.updateCount(id, count);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }
}
